<template>
	<view class="pay-button" @click="doPay">
		<slot></slot>
	</view>
</template>

<script>
	import TokenManager from '@/mpf/token-manager';
	import payUtils from '@/utils/payutils'
	export default {
		name:"pay-button",
		props:{
			orderNo: {
				type: String
			},
			auto:{
				type:Boolean
			}
		},
		data() {
			return {
				submitting: false
			};
		},
		mounted() {
			if(this.auto){
				this.doPay();
			}
		}, 
		methods:{
			/**
			 * 支付
			 */
			doPay(){
				if(this.submitting){
					return ;
				}
				this.submitting = true;
				this.$api.payService.unifiedPay({
					order_no: this.orderNo,
					pay_type: 'weapp',
					open_id: TokenManager.getToken().open_id
				}).then(res => {
					payUtils.evoke({
						orderInfo: res,
						success:(res) => {
							this.$emit('success', this.orderNo);
						},
						fail:(err) => {
							global.toast('未支付');
						}
					})
				}).catch(err => {
					global.toast(err.message || '请求支付失败');
				}).finally(() => {
					console.log('pay request finally')
					this.submitting = false;
				});
						
				
			},
		}
	}
</script>

<style scoped lang="scss">
	.pay-button{
		text-align: center;
	}
</style>